<?php namespace Common\Model;
use Hdphp\Model\Model;
class sc_detail extends Model{
	//指定表
	protected $table="sc_detail";
	
	//自动完成
	protected $auto=array(
	    array('small','getSmall','method',3,3),
	    array('big','getBig','method',3,3),
	   //array('sc_goods_gid','getGgid','method',3,3)
	);
	
	//添加商品详情
	public function addDetail($gid){
		$_POST['sc_goods_gid']=$gid;
		if(!$this->create()) return false;
		$this->add(); return true;
			
	}
	
	//修改商品详情
	public function saveOneDetail(){
		if(!$this->create())  return false;
		$gid=(Q('post.gid'));
		$this->where('sc_goods_gid',$gid)->save();	
	
		if(Q('post.detBig')!==null){ 
			$pics=Q('post.oldDetPic');
			if($pics){
				foreach($pics as $v){
				$pic=pathinfo($v);
				$small=str_replace("_big.{$pic['extension']}", "_small.{$pic['extension']}", $v);
				unlink($v);
				unlink($small);
		        }		
			}    
        }
		return true;
	}
	
	
	
	//获取商品详情
	public function getOneDetail(){
		$gid=Q('get.gid');
		return $this->where('sc_goods_gid',$gid)->find();
	}
	
	
	
	//自动完成小图
	public function getSmall(){
		//获得post传递图片
		$oldpic=Q('post.oldDetPic');
		$pic=$this->getPics();
        if($pic){
	        $small=[];
			foreach($pic as $v){
				
				$pics=pathinfo($v);
				$thumbImg=str_replace(".{$pics['extension']}", "_small.{$pics['extension']}", $v);
			    $small[]=Image::thumb($v,$thumbImg,55,55,6);	  
			}
			$small=implode("|", $small);
			return $small;
        }

       if($oldpic){
       	    $small=[];
			$small=implode("|", $oldpic);
			return $small;
       }  
	   return '';
	}
	
	//自动完成大图
	public function getBig(){
		//获得post传递图片
		$oldpic=Q('post.oldDetPic');
		$pic=$this->getPics();
        if($pic){
        	$big=[];
			foreach($pic as $v){
				
				$pics=pathinfo($v);
				$thumbImg=str_replace(".{$pics['extension']}", "_big.{$pics['extension']}", $v);
			    $big[]=Image::thumb($v,$thumbImg,480,480,6);	
				unlink($v);  
			}
			$big=implode("|", $big);
			return $big;
        }
		
		if($oldpic){
			$big=[];
			$big=implode("|", $oldpic);
			return $big;
		}
		return '';
	}
	
	//获取post传递来的图片数组
	private function getPics(){
		return Q('post.detBig');
	}
	

}






?>